# coding=utf8
import jieba

import libs.NavieBayes as naiveBayes
import random
import numpy as np

from libs import get_root_path

base_path = get_root_path('data')


def simpleTest(content):
    # 加载训练好的模型信息
    vocabularyList, pWordsSpamicity, pWordsHealthy, pSpam = \
        naiveBayes.getTrainedModelInfo()

    smsType = naiveBayes.classify(vocabularyList, pWordsSpamicity,
                                  pWordsHealthy, pSpam, list(jieba.cut(content)))
    print(smsType)


def testClassifyErrorRate():
    """
    测试分类的错误率
    :return:
    """
    filename = base_path + 'training/AdCollection.txt'
    smsWords, classLables = naiveBayes.loadSMSData(filename)

    # 交叉验证
    testWords = []
    testWordsType = []

    testCount = 300
    for i in range(testCount):
        randomIndex = int(random.uniform(0, len(smsWords)))
        testWordsType.append(classLables[randomIndex])
        testWords.append(smsWords[randomIndex])
        del (smsWords[randomIndex])
        del (classLables[randomIndex])

    vocabularyList, pWordsSpamicity, pWordsHealthy, pSpam = \
        naiveBayes.getTrainedModelInfo()

    errorCount = 0.0
    for i in range(testCount):
        smsType = naiveBayes.classify(vocabularyList, pWordsSpamicity,
                                      pWordsHealthy, pSpam, testWords[i])
        print('预测类别：', smsType, '实际类别：', testWordsType[i])
        if smsType != testWordsType[i]:
            errorCount += 1

    print('错误个数：', errorCount, '错误率：', errorCount / testCount)


def main():
    content = '点击 上方蓝字 一节瑜伽课！'
    simpleTest(content)


if __name__ == '__main__':
    main()
    # testClassifyErrorRate()
